import Vue from 'vue';
import Router from 'vue-router'
import Index from './views/index.vue'
import Register from './views/register.vue'
import Login from './views/login.vue'
import NotFound from './views/404.vue'
import Home from './views/home.vue'
import Infoshow from './views/infoshow.vue'
import Fundlist from './views/fundlist.vue'

Vue.use(Router);

const router = new Router({
    mode:'history',
    base: process.env.BASE_URL,
    routes:[
        {
            path:'/',
            redirect:'/index'
        },
        {
            path:'/index',
            component:Index,
            name:'index',
            children: [
                {path: '', component:Home},
                {path: '/home', component:Home},
                {path: '/infoshow', component:Infoshow},
                {path: '/fundlist', component:Fundlist},
            ]
        },
        {
            path:'/register',
            component:Register,
            name:'register'
        },
        {
            path:'/login',
            component:Login,
            name:'login'
        },
        {
            path:'*',
            component:NotFound,
            name:'404'
        }
    ]
});

// 路由守卫
router.beforeEach((to, from, next) => {
   const isLogin = localStorage.eleToken ? true : false;
   if(to.path == '/login' || to.path == '/register') {
       next();
   }else {
       isLogin ? next() : next('/login');
   }
});

export default router;